package com.cheer.controller;

import com.cheer.common.Result;
import com.cheer.entity.OaRuleEntity;
import com.cheer.service.AuthService;
import com.cheer.service.OaRuleService;
import com.cheer.vo.OaLevelVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpSession;
import java.time.Duration;
import java.util.List;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/rule")
public class OaRuleController {

    @Autowired
    private OaRuleService oaRuleService;

    @Autowired
    private AuthService authService;

    @Autowired
    private ThreadPoolTaskScheduler taskScheduler;

    @PostConstruct
    public void init() {
        taskScheduler.scheduleWithFixedDelay(() -> oaRuleService.loadRules(), Duration.ofMinutes(5));
    }

    @ApiOperation("查询评定规则")
    @GetMapping("/info")
    public Result<List<OaRuleEntity>> info(HttpSession session) {
        authService.getUserId(session);
        List<OaRuleEntity> rules = oaRuleService.listRules();
        return Result.success(rules);
    }

    @ApiOperation("查询我的等级")
    @GetMapping("/level")
    public Result<OaLevelVO> level(HttpSession session) {
        OaLevelVO levelVO = oaRuleService.getLevel(authService.getUserId(session), authService.getOpenid(session));
        return Result.success(levelVO);
    }

}
